package com.hy.admin.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hy.admin.entity.CourseSeriesEntity;
import com.hy.admin.helper.request.CourseSeriesRequest;
import com.hy.admin.helper.request.PersonalRequest;
import com.hy.admin.helper.response.MemberIncomeDetailResponse;
import com.hy.admin.service.CourseSeriesService;
import com.hy.admin.service.IncomeService;
import com.hy.core.request.ApiRequestDTO;
import com.hy.core.response.ApiResponseDTO;
import com.hy.core.response.ServicesResult;
import org.apache.commons.httpclient.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by yangqq on 18/4/3.
 */
@RestController
@RequestMapping("/api/series")
public class CourseSeriesController {

    protected final Logger logBase = LoggerFactory.getLogger(CourseSeriesController.class);
    @Autowired
    private CourseSeriesService courseSeriesService;

    /**
     * 新增课程系列
     * @param request
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ApiResponseDTO add(@RequestBody final ApiRequestDTO apiRequestDTO,HttpServletRequest request) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("series_add_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        //获取请求对象
        CourseSeriesRequest courseSeriesRequest= gson.fromJson(apiRequestDTO.getRequestData(), CourseSeriesRequest.class);
        //获取请求数据
        ServicesResult<Boolean> seriesResponse = courseSeriesService.add(courseSeriesRequest.getMemberId(), courseSeriesRequest.getName());
        apiResponseDTO.initServicesResult(seriesResponse);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(seriesResponse.getResult()));
        logBase.info("series_add_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    /**
     * 修改课程系列
     * @param request
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ApiResponseDTO update(@RequestBody final ApiRequestDTO apiRequestDTO,HttpServletRequest request) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("series_update_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        //获取请求对象
        CourseSeriesRequest courseSeriesRequest= gson.fromJson(apiRequestDTO.getRequestData(), CourseSeriesRequest.class);
        //获取请求数据
        ServicesResult<Boolean> seriesResponse = courseSeriesService.update(courseSeriesRequest.getSeriesId(), courseSeriesRequest.getName());
        apiResponseDTO.initServicesResult(seriesResponse);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(seriesResponse.getResult()));
        logBase.info("series_update_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    /**
     * 查询某教师的所有课程系列
     * @param request
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiResponseDTO list(@RequestBody final ApiRequestDTO apiRequestDTO,HttpServletRequest request) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("series_list_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        //获取请求对象
        CourseSeriesRequest courseSeriesRequest= gson.fromJson(apiRequestDTO.getRequestData(), CourseSeriesRequest.class);
        //获取请求数据
        ServicesResult<List<CourseSeriesEntity>> seriesResponse = courseSeriesService.listByMid(courseSeriesRequest.getMemberId());
        apiResponseDTO.initServicesResult(seriesResponse);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(seriesResponse.getResult()));
        logBase.info("series_list_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }
}
